package com.nhim.utility;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 문자열 관련 유틸리티
 * @author wonsama
 * @since 2011.12.29
 */
public class WStringUtil {

	/**
	 * RPAD : RIGHT PADDING : padingChar '0'
	 * @param source SOURCE STRING
	 * @param size SENTENCE SIZE 
	 * @return PADDING SENTENCE
	 */
	public static String rPad(String source, int size)
	{
		return rPad( source , size , '0' );
	}
	
	/**
	 * RPAD : RIGHT PADDING
	 * @param source SOURCE STRING
	 * @param size SENTENCE SIZE 
	 * @param padingChar PADDING STRING

	 * @return PADDING SENTENCE
	 */
	public static String rPad(String source, int size, char padingChar)
	{
		int len = size - source.length();
		
		StringBuffer sb = new StringBuffer();
		sb.append( source );
		for (int i=0; i < len ; i++){
			sb.append( padingChar );
		}
		
		return sb.toString();
	}
	
	
	/**
	 * LPAD : LEFT PADDING : padingChar '0'
	 * @param source SOURCE STRING
	 * @param size SENTENCE SIZE 
	 * @return PADDING SENTENCE
	 */
	public static String lPad(String source, int size)
	{
		return lPad( source , size , '0' );
	}
	
	/**
	 * LPAD : LEFT PADDING
	 * @param source SOURCE STRING
	 * @param size SENTENCE SIZE 
	 * @param padingChar PADDING CHARACTER
	 * @return PADDING SENTENCE
	 */
	public static String lPad(String source, int size, char padingChar)
	{
		int len = size - source.length();
		
		StringBuffer sb = new StringBuffer();
		for (int i=0; i < len ; i++){
			sb.append( padingChar );
		}
		sb.append( source );
		
		return sb.toString();
	}

	/**
	 * JOIN LIST WORDS
	 * @param source SOURCE SENTENCE
	 * @param joinWord JOIN WORD
	 * @return JOINED SENTENCE
	 */
	public static String join(String[] source, String joinWord)
	{

		StringBuffer sb = new StringBuffer();
		for(String v:source){
			sb.append( v );
			sb.append( joinWord );
		}

		//remove last join word
		int lastIdx = sb.lastIndexOf(joinWord);
		sb.delete(lastIdx ,sb.length());

		return sb.toString();
	}
	
	/**
	 * JOIN LIST WORDS
	 * @param source SOURCE SENTENCE
	 * @param joinWord JOIN WORD
	 * @return JOINED SENTENCE
	 */
	public static String join(List<String> source, String joinWord)
	{

		StringBuffer sb = new StringBuffer();
		for(String v:source){
			sb.append( v );
			sb.append( joinWord );
		}

		//remove last join word
		int lastIdx = sb.lastIndexOf(joinWord);
		sb.delete(lastIdx ,sb.length());

		return sb.toString();
	}
	
	/**
	 * JOIN LIST WORDS
	 * @param source SOURCE SENTENCE
	 * @param joinWord JOIN WORD
	 * @param fixWord PREFIX & SUFFIX
	 * @return JOINED SENTENCE
	 */
	public static String join(List<String> source, String joinWord, String fixWord)
	{

		StringBuffer sb = new StringBuffer();
		for(String v:source){
			sb.append( fixWord );
			sb.append( v );
			sb.append( fixWord );
			sb.append( joinWord );
		}

		//remove last join word
		int lastIdx = sb.lastIndexOf(joinWord);
		sb.delete(lastIdx ,sb.length());

		return sb.toString();
	}
	
	/**
	 * JOIN LIST WORDS
	 * @param source SOURCE SENTENCE
	 * @param joinWord JOIN WORD
	 * @param fixWord PREFIX & SUFFIX
	 * @return JOINED SENTENCE
	 */
	public static String join(String[] source, String joinWord, String fixWord)
	{

		StringBuffer sb = new StringBuffer();
		for(String v:source){
			sb.append( fixWord );
			sb.append( v );
			sb.append( fixWord );
			sb.append( joinWord );
		}

		//remove last join word
		int lastIdx = sb.lastIndexOf(joinWord);
		sb.delete(lastIdx ,sb.length());

		return sb.toString();
	}
	
	/**
	 * SPLIT TO STRING LIST 
	 * @param source STRING SOURCE
	 * @param regex REGEX
	 * @return SPLIT STRING LIST
	 */
	public static List<String> splitToList(String source, String regex)
	{
		String[] _list = source.split( regex );
		List<String> list = new ArrayList<String>(_list.length);
		
		for(String s:_list){
			list.add(s);
		}
		
		return list;
	}
	
	/**
	 * SPLIT TO STRING LIST 
	 * @param source STRING SOURCE
	 * @param regex REGEX
	 * @return SPLIT STRING LIST
	 */
	public static List<String> splitToListWithTrim(String source, String regex)
	{
		String[] _list = source.split( regex );
		List<String> list = new ArrayList<String>(_list.length);
		
		for(String s:_list){
			list.add(s.trim());
		}
		
		return list;
	}

	/**
	 * CHECK SOURCE INCLUDE FIND WORDS
	 * @param source SOURCE SENTENCE
	 * @param find FIND WORDS
	 * @return HAS CONTAIN WORD 
	 */
	public static boolean hasContain(String source, List<String> find)
	{
		for( String v:find ){
			if( source.indexOf( v ) >= 0 )
				return true;
		}
		return false;
	}
	
}
